package com.tools.cleanmaster.manager;

import android.content.Intent;
import android.os.BatteryManager;
import android.os.Message;
import android.text.TextUtils;

import com.tools.ad.log.MLog;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.R;


/**
 * desc:
 * date: 2018/4/16
 * author: ancun
 */

public class ChargingManager {

    public static final int FLAG_TURN_SCREEN_ON = 0;
    public static final int FLAG_TURN_SCREEN_OFF = 1;

    /**
     * 应用上下文
     */
    private CleanApplication imContext;

    /**
     * 当前电量百分比
     */
    private int mBatteryPct;

    /**
     * 当前电量温度
     */
    private float mBatteryTemp = 31;

    /**
     * 是否正在充电
     */
    private boolean mIsCharging;

    public ChargingManager(CleanApplication antivirusApp) {
        imContext = antivirusApp;
    }

    public void updateChargingState(Intent intent) {
        if (intent == null) {
            return;
        }
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            return;
        }

        switch (action) {
            case Intent.ACTION_BATTERY_CHANGED://电量发生改变
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_BATTERY_CHANGED");
                }

                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); //电量的刻度
                mBatteryPct = (int) (level / (float) scale * 100);

                int batteryTemp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
                mBatteryTemp = batteryTemp / 10f;

                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
                int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> mBatteryPct : " + mBatteryPct);
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> mBatteryTemp : " + mBatteryTemp);
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> status : " + status);
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> plug : " + chargePlug);
                }

                mIsCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
                boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
                boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> isCharging : " + mIsCharging);
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> usbCharge : " + usbCharge);
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> acCharge : " + acCharge);
                    MLog.e("BatteryBroadcastReceiver", "----------------------------------------");
                }

                break;

            case Intent.ACTION_BATTERY_LOW://电量低
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_BATTERY_LOW");
                }
                break;

            case Intent.ACTION_BATTERY_OKAY://电量充满
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_BATTERY_OKAY");
                }
                break;

            case Intent.ACTION_POWER_CONNECTED://接通电源
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_POWER_CONNECTED");
                }
                mIsCharging = true;
                showSmartChargingView(FLAG_TURN_SCREEN_ON);
                break;

            case Intent.ACTION_POWER_DISCONNECTED://拔出电源
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_POWER_DISCONNECTED");
                }
                mIsCharging = false;
                showSmartChargingView(FLAG_TURN_SCREEN_ON);
                break;

            case Intent.ACTION_USER_PRESENT:
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_USER_PRESENT");
                }
                break;

            case Intent.ACTION_SCREEN_ON:
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_SCREEN_ON");
                }
                if (mIsCharging) {
                    showSmartChargingView(FLAG_TURN_SCREEN_ON);
                }
                break;

            case Intent.ACTION_SCREEN_OFF:
                if (MLog.debug) {
                    MLog.e("BatteryBroadcastReceiver", "onReceive--> ACTION_SCREEN_OFF");
                }
                if (mIsCharging) {
                    showSmartChargingView(FLAG_TURN_SCREEN_OFF);
                }
                break;
        }

    }

    private void showSmartChargingView(int flag) {
        Message message = Message.obtain();
        message.what = R.id.msg_cleaner_show_smart_charging_view;
        message.arg2 = flag;
        imContext.handleMobMessage(message);
    }

    private void hideSmartChargingView() {
        imContext.handleMobEmptyMessage(R.id.msg_cleaner_hide_smart_charging_view);
    }

    public int getBatteryPct() {
        return mBatteryPct;
    }

    public void setBatteryPct(int batteryPct) {
        mBatteryPct = batteryPct;
    }

    public float getBatteryTemp() {
        return mBatteryTemp;
    }

    public void setBatteryTemp(float batteryTemp) {
        mBatteryTemp = batteryTemp;
    }

    public void setChargingEnable(boolean chargingEnable) {
        imContext.getCleanSharedPrefManager().setChargingEnable(chargingEnable);
    }

    public boolean getChargingEnable() {
        return imContext.getCleanSharedPrefManager().getChargingEnable();
    }
}
